﻿package com.victim.snake.snake
{

	import flash.geom.Rectangle;
	import com.victim.snake.user.User;
	import flash.geom.Matrix;



	//主成贪吃蛇的蛇身体类
	public class Body
	{
		//常量
		public static const left:uint = 37;
		public static const up:uint = 38;
		public static const right:uint = 39;
		public static const down:uint = 40;
		public static const colors:Array=new Array(0xff0000,0x0000ff,0xffff00,0x008080,0xff00ff,0xCC66FF,0xc0c0c0);
		
		
		//蛇身体信息
		private var serial:uint;	//身体序号
		private var rect:Rectangle;	//身体方块
		private var color:int;	//颜色
		private var direction:uint;	//蛇的方向
		private var speed:uint;	//速度
		
		public var matrix:Matrix=new Matrix();	//用于旋转或放大		
		private var isTrun:Boolean=false;	//是否转向身体标记
		private var prvDirection:uint;	//上一方向值
		
		

		//构造函数,初始化
		public function Body(x:Number,y:Number,w:Number,h:Number,color:uint,direction:uint,serial:uint)
		{
			this.serial=serial;	//初使化身体序号
			this.rect = new Rectangle(x,y,w,h);	//初使化蛇身体位置
			this.color = color;	//初使化蛇的颜色
			this.direction = direction;	//初使化蛇的方向
		}

		//按方向给出身体旋转角度的Matrix
		public function rotate():Matrix{
			//this.matrix.a=0;
			//this.matrix.b=0;
			//this.matrix.c=0;
			//this.matrix.d=0;
			switch(this.direction){
				case 37:
					this.matrix.rotate((90/180)*Math.PI);
					break;
				case 38:
					this.matrix.rotate((90/180)*Math.PI);
					break;
				case 39:
					this.matrix.rotate((180/180)*Math.PI);
					break;
				case 40:
					this.matrix.rotate((270/180)*Math.PI);
					break;
			}
			return this.matrix;
		}
		

		//移动蛇身体
		public function move()
		{
			//按指定方向移动
			switch (this.direction)
			{
				case Body.up :
					this.rect.y=this.rect.y-this.rect.height;
					break;
				case Body.down :
					this.rect.y=this.rect.y+this.rect.height;
					break;
				case Body.left :
					this.rect.x=this.rect.x-this.rect.width;
					break;
				case Body.right :
					this.rect.x=this.rect.x+this.rect.width;
					break;
			}
		}
		
		//移动蛇身体（坐标）
		public function moveByCoordinate(x:Number,y:Number)
		{
			this.rect.x=x;
			this.rect.y=y;
		}

		//get and set
		public function getX():Number
		{
			return this.rect.x;
		}

		public function setX(x:Number)
		{
			this.rect.x = x;
		}

		public function getY():Number
		{
			return this.rect.y;
		}

		public function setY(y:Number)
		{
			this.rect.y = y;
		}

		public function getWidth():Number
		{
			return this.rect.width;
		}

		public function setWidth(width:Number)
		{
			this.rect.width = width;
		}

		public function getHeight():Number
		{
			return this.rect.height;
		}

		public function setHeight(height:Number)
		{
			this.rect.height = height;
		}

		public function getColor():uint
		{
			return this.color;
		}

		public function setColor(color:uint)
		{
			this.color = color;
		}
		
		public function getSerial():uint
		{
			return this.serial;
		}

		public function setSerial(serial:uint)
		{
			this.serial = serial;
		}

		
		public function getDirection():uint
		{
			return this.direction;
		}
				
		public function getPrvDirection():uint
		{
			return this.prvDirection;
		}
		
		public function setDirection(direction:uint):void
		{
			this.prvDirection=this.direction;	//设置转向前的值
			this.direction = direction;	//转向
		}
		
		public function getSpeed():uint
		{
			return this.speed;
		}
				
		public function setSpeed(speed:uint)
		{
			this.speed = speed;
		}
		
		public function setIsTrun(bol:Boolean):void{
			this.isTrun=bol;
		}
		
		public function getIsTrun():Boolean{
			return this.isTrun;
		}
		
	}
}